package com.ccys.app.controller;

import com.ccys.app.service.PaymentDetailService;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.app.paymentDetails.CalculationCostDto;
import com.ccys.db.dto.app.paymentDetails.CheckPromoCodeDto;
import com.ccys.db.mapper.TblSupplierMapper;
import com.ccys.db.vo.app.paymentDetails.CalculationCostVo;
import com.ccys.db.vo.app.paymentDetails.GetCardListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：XLY
 * @Date：2024/1/28 0028
 */
@RestController
@RequestMapping("/paymentDetails")
@Api(tags = "Payment Details (GP)")
public class PaymentDetailsController {


    @Resource
    private TblSupplierMapper mapper;

    @Resource
    private PaymentDetailService paymentDetailService;


    @ApiOperation("Payment Details (GP)/ 1")
    @GetMapping("GetCardList")
    public ResponseResultApp<List<GetCardListVo>> getCardList() {
        String curLoginUserId = UserUtil.getCurLoginUserId();
        List<GetCardListVo> cardList = mapper.getCardList(curLoginUserId);
        return ResponseResultApp.success(cardList);
    }


    @ApiOperation("Payment Details (GP)/ 2")
    @PostMapping("CheckPromoCode")
    @Transactional
    public ResponseResultApp<CalculationCostVo> checkPromoCode(@RequestBody CheckPromoCodeDto dto) {

        String deliver_type = dto.getDeliver_type();
        if ("deliver".equals(deliver_type)) {
            dto.setPharmacy_id("");
        }
        if ("express".equals(deliver_type)) {
            dto.setDelivery_timeslot_id("");
        }
        if ("1".equals(deliver_type)) {
            dto.setDelivery_timeslot_id("");
            dto.setDelivery_address_id("");
        }

        // check的时候根据这个bookingid，看是小孩问诊还是自己问诊
        String booking_id = dto.getBooking_id();
        if (StringUtils.isNotEmpty(booking_id)) {
            String memId = mapper.getMemberIdByBookingId(booking_id);
            // check这个promocode , 就算是false也要返回数据回去?
            String promo_code = dto.getPromo_code();
            List<Integer> promoCodeCount = mapper.getPromoCodeCount(memId, promo_code);
            int sum = promoCodeCount.stream().mapToInt(x -> x).sum();
            boolean flag = false;
            if (sum > 0) {
                flag = true;
            }
        }
        // 2024/2/4 0004 这个接口调用了API_MemberCalculationCost来做返回值
        // TODO: 2024/2/4 0004 flag的值用来做啥。。。
        CalculationCostDto costDto = new CalculationCostDto();
        BeanUtils.copyProperties(dto,costDto);
        CalculationCostVo res = paymentDetailService.calculationCost(costDto);
        return ResponseResultApp.success(res);
    }


    @ApiOperation("Payment Details (GP)/ 3")
    @PostMapping("CalculationCost")
    @Transactional
    public ResponseResultApp<CalculationCostVo> calculationCost(@RequestBody CalculationCostDto dto) {
        CalculationCostVo res = paymentDetailService.calculationCost(dto);
        return ResponseResultApp.success(res);
    }


    @ApiOperation("Payment Details (GP)/ 6")
    @GetMapping("CompleteBooking")
    public ResponseResultApp completeBooking() {

        return ResponseResultApp.success();
    }
}
